<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryFormRef" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="用户账号" prop="userName">
        <el-input
          v-model="queryParams.userName"
          placeholder="请输入用户账号"
          clearable
        />
      </el-form-item>
      <el-form-item label="手机号码" prop="phone">
        <el-input
          v-model="queryParams.phone"
          placeholder="请输入手机号码"
          clearable
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="small"
          @click="handleAdd"
          v-hasPermi="['member:user:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="small"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['member:user:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="small"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['member:user:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="small"
          @click="handleExport"
          v-hasPermi="['member:user:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar v-model:showSearch="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table
      v-loading="loading"
      :data="userList"
      :default-sort="tableSort"
      @sort-change="handleSortChange"
      @selection-change="handleSelectionChange"
    >
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="ID" align="center" prop="id" />
      <el-table-column label="用户账号" align="center" prop="userName" />
      <el-table-column label="用户昵称" align="center" prop="nickName" />
      <el-table-column label="头像" align="center" prop="avatar" >

      </el-table-column>
      <el-table-column label="手机号码" align="center" prop="phone" />
      <!-- <el-table-column label="用户邮箱" align="center" prop="email" /> -->
      <el-table-column label="用户性别" align="center" prop="sex" >
        <template #default="scope">
            <dict-tag :options="sys_user_sex" :value="scope.row.sex" />
        </template>
      </el-table-column>
      <el-table-column label="openId" align="center" prop="openId" />
      <el-table-column label="帐号状态" align="center" prop="status" >
        <template #default="scope">
            <dict-tag :options="sys_normal_disable" :value="scope.row.status" />
        </template>
      </el-table-column>
      <el-table-column label="备注" align="center" prop="remark" />
      <el-table-column label="登录时间" align="center" prop="loginTime" width="180" >
      </el-table-column>
      <el-table-column label="创建时间" align="center" prop="createTime" width="180" sortable="custom">
      </el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template #default="scope">
          <el-button
            size="small"
            type="text"
            icon="el-icon-edit"
            @click="handleDetail(scope.row)"
            v-hasPermi="['member:user:edit']"
          >详情</el-button>
          <el-button
            size="small"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['member:user:edit']"
          >修改</el-button>
          <el-button
            size="small"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['member:user:remove']"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page="queryParams.pageNum"
      :limit="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改用户信息对话框 -->
    <el-dialog v-model="dialogVisible" :title="dialogTitle" width="500px" append-to-body  >
      <el-form ref="formRef" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="用户账号" prop="userName">
          <el-input v-model="form.userName" placeholder="请输入用户账号" />
        </el-form-item>
        <el-form-item label="用户昵称" prop="nickName">
          <el-input v-model="form.nickName" placeholder="请输入用户昵称" />
        </el-form-item>
        <el-form-item label="手机号码" prop="phone">
          <el-input v-model="form.phone" placeholder="请输入手机号码" />
        </el-form-item>
        <el-form-item label="用户邮箱" prop="email">
          <el-input v-model="form.email" placeholder="请输入用户邮箱" />
        </el-form-item>
        <el-form-item label="头像" prop="avatar">
          <el-input v-model="form.avatar" placeholder="请输入头像" />
        </el-form-item>
        <el-form-item label="密码" prop="userPassword">
          <el-input v-model="form.userPassword" placeholder="请输入密码" />
        </el-form-item>
        <el-form-item label="微信openId" prop="openId">
          <el-input v-model="form.openId" placeholder="请输入微信openId" />
        </el-form-item>
        <el-form-item label="分类状态">
            <el-radio-group v-model="form.status">
              <el-radio
                  v-for="dict in sys_normal_disable"
                  :key="dict.value"
                  :label="dict.value"
              >{{ dict.label }}</el-radio>
            </el-radio-group>
        </el-form-item>
        <el-form-item label="备注" prop="remark">
          <el-input v-model="form.remark" placeholder="请输入备注" />
        </el-form-item>
      </el-form>
      <template #footer>
          <div class="dialog-footer">
            <el-button @click="handleDialogCancel">取 消</el-button>
            <el-button type="primary" @click="submitForm">确 定</el-button>
          </div>
      </template>
    </el-dialog>
    <!-- 添加或修改用户信息对话框 end -->


    <!-- 用户信息详情对话框 -->
    <el-dialog v-model="detailDialogVisible" title="用户信息详情" width="800px" append-to-body  >
    <el-descriptions title="用户信息">
      <el-descriptions-item label="ID">{{form.id}}</el-descriptions-item>
      <el-descriptions-item label="用户账号">{{form.userName}}</el-descriptions-item>
      <el-descriptions-item label="用户昵称">{{form.nickName}}</el-descriptions-item>
      <el-descriptions-item label="手机号码">{{form.phone}}</el-descriptions-item>
      <el-descriptions-item label="用户邮箱">{{form.mail}}</el-descriptions-item>
      <el-descriptions-item label="用户性别">
        <dict-tag :options="sys_user_sex" :value="form.sex" />
      </el-descriptions-item>
      <el-descriptions-item label="头像">{{form.avatar}}</el-descriptions-item>
      <el-descriptions-item label="帐号状态">
        <dict-tag :options="sys_normal_disable" :value="form.status" />
      </el-descriptions-item>
      <el-descriptions-item label="备注">{{form.remark}}</el-descriptions-item>
      <el-descriptions-item label="登录时间">{{form.loginTime}}</el-descriptions-item>
      <el-descriptions-item label="创建时间">{{form.createTime}}</el-descriptions-item>
  </el-descriptions>
      <template #footer>
          <div class="dialog-footer">
            <el-button @click="detailDialogVisible = false">关 闭</el-button>
          </div>
      </template>
    </el-dialog>
    <!-- 用户信息详情对话框 end -->

  </div>
</template>

<script setup name="User">
import { ref, toRefs } from "vue";
import { listUser, getUser, delUser, addUser, updateUser, exportXlsUrl } from "@/api/member/user";

const { proxy } = getCurrentInstance();

const { sys_normal_disable } = proxy.useDictNum("sys_normal_disable");
const { sys_user_sex } = proxy.useDictNum("sys_user_sex");

  // 遮罩层
  const loading = ref(true);
  // 选中数组
  const selectedIds = ref([]);
  // 非单个禁用
  const single = ref(true);
  // 非多个禁用
  const multiple = ref(true);
  // 显示搜索条件
  const showSearch = ref(true);
  // 总条数
  const total = ref(0);
  // 用户信息表格数据
  const userList  = ref([]);
  // 弹出层标题
  const dialogTitle  = ref("");
  // 是否显示弹出层
  const dialogVisible  = ref(false);
  const detailDialogVisible  = ref(false);
  // 查询参数
     const reactiveData  = reactive(
     {
        queryParams: {
          pageNum: 1,
          pageSize: 10,
        userName: null,
        phone: null,
        status: null,
            params: {
              sort: 'createTime',
              order: 'descending',
            }
        },
        form: {},
        rules: {
        userName: [
          { required: true, message: "用户账号不能为空", trigger: "blur" }
        ],
        nickName: [
          { required: true, message: "用户昵称不能为空", trigger: "blur" }
        ],
        createBy: [
          { required: true, message: "创建人不能为空", trigger: "blur" }
        ],
        updateBy: [
          { required: true, message: "修改人不能为空", trigger: "blur" }
        ],
        yn: [
          { required: true, message: "0无效1有效不能为空", trigger: "blur" }
        ]
        }
     });

      const {queryParams,form,rules} = toRefs(reactiveData);

      const tableSort = computed(()=>{
        if(queryParams.value.params.sort){
          return {
            prop: queryParams.value.params.sort,
            order: queryParams.value.params.order
          }
        }
        return null;
      })

    /** 查询用户信息列表 */
   function getList() {
     loading.value = true;
      listUser(queryParams.value).then(response => {
        userList.value = response.data.records;
        total.value = response.data.total;
        loading.value = false;
      });
    }
    // 取消按钮
    function handleDialogCancel() {
      dialogVisible.value = false;
      reset();
    }
    // 表单重置
    function reset() {
      form.value = {
        id: null,
        userName: null,
        nickName: null,
        phone: null,
        email: null,
        sex: null,
        avatar: null,
        userPassword: null,
        openId: null,
        status: "0",
        remark: null,
        loginTime: null,
        createTime: null,
        createBy: null,
        updateTime: null,
        updateBy: null,
        yn: null
      };
      proxy.resetForm("formRef");
    }
    /** 搜索按钮操作 */
   function handleQuery() {
      queryParams.value.pageNum = 1;
      getList();
    }
    /** 重置按钮操作 */
    function resetQuery() {
      proxy.resetForm("queryFormRef");
      handleQuery();
    }

    // 多选框选中数据
    function handleSelectionChange(selection) {
      selectedIds.value = selection.map(item => item.id)
      single.value = selection.length!==1
      multiple.value = !selection.length
    }

    // 排序
    function handleSortChange({ column, prop, order }){
      queryParams.value.params.sort = prop?prop:null;
      queryParams.value.params.order = order?order:null;
      getList();
    }

    /** 新增按钮操作 */
    function handleAdd() {
      reset();
      dialogVisible.value = true;
      dialogTitle.value = "添加用户信息";
    }

    /** 详情按钮操作 */
    function handleDetail(row) {
      reset();
      const id = row.id || selectedIds.value
      getUser(id).then(response => {
        form.value = response.data;
        detailDialogVisible.value = true;
      });
    }

    /** 修改按钮操作 */
    function handleUpdate(row) {
      reset();
      const id = row.id || selectedIds.value
      getUser(id).then(response => {
        form.value = response.data;
        dialogVisible.value = true;
        dialogTitle.value = "修改用户信息";
      });
    }
    /** 提交按钮 */
    function submitForm() {
      proxy.$refs["formRef"].validate(valid => {
        if (valid) {
          if (form.value.id != null) {
            updateUser(form.value).then(response => {
              proxy.$modal.msgSuccess("修改成功");
              dialogVisible.value = false;
              getList();
            });
          } else {
            addUser(form.value).then(response => {
              proxy.$modal.msgSuccess("新增成功");
              dialogVisible.value = false;
              getList();
            });
          }
        }
      });
    }
    /** 删除按钮操作 */
   function handleDelete(row) {
      const delids = row.id || selectedIds.value;
      proxy.$modal.confirm('是否确认删除用户信息编号为"' + delids + '"的数据项？').then(function() {
        return delUser(delids);
      }).then(() => {
        getList();
        proxy.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    }
   /** 导出按钮操作 */
   function handleExport() {
      proxy.download(exportXlsUrl, {
        ...queryParams.value
      }, `user_${new Date().getTime()}.xlsx`)
    }
   /** 初始化数据 **/
   getList();
</script>
